package airport.client;

import airport.client.utils.Constantes;
import airport.client.utils.HTML_A;
import airport.client.widget.accueil.AccueilWB;
import airport.client.widget.connexion.ConnexionWB;
import airport.client.widget.hotel.HotelWB;
import airport.client.widget.notfound.NotFoundWB;
import airport.client.widget.panneau.PanneauWB;
import airport.client.widget.vol.VolWB;
import airport.shared.beans.UserBean;
import airport.shared.service.AirportService;
import airport.shared.service.AirportServiceAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>
 */
public class Airport extends Composite implements EntryPoint {

	public interface Resources extends ClientBundle {
		@ClientBundle.Source("loading.gif")
		ImageResource getPreloader();
	}

	Resources resources = GWT.create(Resources.class);

	protected static FlowPanel panelHTML = new FlowPanel();
    static FlowPanel panelBody = new FlowPanel();

	protected static FlowPanel infoPanelDanger = new FlowPanel();
	protected static FlowPanel infoPanelSuccess = new FlowPanel();
	protected static PopupPanel PatienterPopup = new PopupPanel();

	FlowPanel navbar = new FlowPanel();
	FlowPanel navbarInner = new FlowPanel();
	FlowPanel container = new FlowPanel();
	FlowPanel widgets = new FlowPanel();
	FlowPanel widget = new FlowPanel();
    FlowPanel push = new FlowPanel();
    FlowPanel footer = new FlowPanel();
    FlowPanel footerContainer = new FlowPanel();
    FlowPanel footerLabel = new FlowPanel();
    FlowPanel facebookA = new FlowPanel("a");
    FlowPanel facebookI = new FlowPanel("i");
    FlowPanel twitterA = new FlowPanel("a");
    FlowPanel twitterI = new FlowPanel("i");
    FlowPanel googleA = new FlowPanel("a");
    FlowPanel googleI = new FlowPanel("i");
    FlowPanel linkelA = new FlowPanel("a");
    FlowPanel linkelI = new FlowPanel("i");
    FlowPanel center = new FlowPanel("center");

    static FlowPanel span2 = new FlowPanel();
    static FlowPanel span10 = new FlowPanel();
	HTML_A projectName = new HTML_A(Constantes.PROJECT_NAME);
	FlowPanel navCollapse = new FlowPanel();
	FlowPanel navbarForm = new FlowPanel();
    public static FlowPanel labelTitre = new FlowPanel("span");
    static HTML_A btnConnexion = new HTML_A(Constantes.CONNEXION);
    static HTML_A btnDeconnexion = new HTML_A(Constantes.DECONNEXION);

	static FlowPanel sidebar = new FlowPanel();
    FlowPanel navlist = new FlowPanel("ul");
    FlowPanel navHeadVol = new FlowPanel("li");

    FlowPanel dividerVol = new FlowPanel("li");

    FlowPanel navHeadPanneau = new FlowPanel("li");

    FlowPanel dividerPanneau = new FlowPanel("li");

    FlowPanel navHeadHotel = new FlowPanel("li");

    FlowPanel ajouterVolLi = new FlowPanel("li");
    HTML_A ajouterVolLink = new HTML_A("Ajouter");

    FlowPanel consulterVolLi = new FlowPanel("li");
    HTML_A consulterVolLink = new HTML_A("Consulter");

    FlowPanel ajouterPanneauLi = new FlowPanel("li");
    HTML_A ajouterPanneauLink = new HTML_A("Ajouter");


    FlowPanel consulterPanneauLi = new FlowPanel("li");
    HTML_A consulterPanneauLink = new HTML_A("Consulter");

    FlowPanel ajouterHotelLi = new FlowPanel("li");
    HTML_A ajouterHotelLink = new HTML_A("Ajouter");

    FlowPanel consulterHotelLi = new FlowPanel("li");
    HTML_A consulterHotelLink = new HTML_A("Consulter");

	public void infoDanger(String info) {
		Window.scrollTo(0, 0);
		infoPanelDanger.setVisible(true);
		infoPanelDanger.getElement().setInnerHTML(info);
	}

	public void infoDangerHide() {
		infoPanelDanger.setVisible(false);
	}

	public void infoSuccess(String info) {
		Window.scrollTo(0, 0);
		infoPanelSuccess.setVisible(true);
		infoPanelSuccess.getElement().setInnerHTML(info);
		Timer t = new Timer() {
			@Override
			public void run() {
				infoPanelSuccess.setVisible(false);
			}
		};
		// Schedule the timer to run once in 3 seconds.
		t.schedule(1000);
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		checkLogin();

	}

	private void initPage() {
		initPatienterPopUp();
		changeHistory(History.getToken());
		widgets.setStyleName("container-fluid");
		widget.setStyleName("row-fluid");
		span2.setStyleName("span2");
		infoPanelSuccess.setStyleName("alert alert-success");
		infoPanelDanger.setStyleName("alert alert-danger");
		infoPanelDanger.setVisible(false);
		infoPanelSuccess.setVisible(false);
		widgets.add(infoPanelDanger);
		widgets.add(infoPanelSuccess);
		widget.add(span2);
		span10.setStyleName("span10 well container-fluid");
        panelBody.setStyleName("row-fluid");
        widget.add(span10);
        widgets.add(widget);
        push.getElement().setAttribute("id", "push");
        widgets.add(push);
        span10.add(panelBody);
		span2.add(initSideBar());
        panelHTML.add(initNavbar());
        panelHTML.add(widgets);
        panelHTML.getElement().setAttribute("id", "wrap");
        footer.getElement().setAttribute("id", "footer");

        facebookA.setStyleName("btn btn-circle btn-facebook");
        facebookI.setStyleName("fa fa-facebook");
        twitterA.setStyleName("btn btn-circle btn-twitter");
        twitterI.setStyleName("fa fa-twitter");
        googleA.setStyleName("btn btn-circle btn-google-plus");
        googleI.setStyleName("fa fa-google-plus");
        linkelA.setStyleName("btn btn-circle btn-linkedin");
        linkelI.setStyleName("fa fa-linkedin");
        facebookA.add(facebookI);
        twitterA.add(twitterI);
        googleA.add(googleI);
        linkelA.add(linkelI);

        footerContainer.add(footerLabel);
        footerLabel.setStyleName("muted credit");
        footerLabel.add(center);
        center.getElement().setInnerHTML("© AdManager 2015 - Rejoignez-nous sur ");
        center.add(facebookA);
        center.add(twitterA);
        center.add(googleA);
        //    center.add(linkelA);
        footer.add(footerContainer);

        footerContainer.setStyleName("container");
        footerContainer.getElement().setAttribute("style", "height: 60px;");

        RootPanel.get().add(panelHTML);
        RootPanel.get().add(footer);

        facebookA.getElement().setAttribute("href", "https://www.facebook.com/AdManagerM2TIIL");
        facebookA.getElement().setAttribute("target", "_blank");

        twitterA.getElement().setAttribute("href", "https://twitter.com/AdManagerM2TIIL");
        twitterA.getElement().setAttribute("target", "_blank");

        googleA.getElement().setAttribute("href", "https://plus.google.com/u/0/104206258493175705526/posts");
        googleA.getElement().setAttribute("target", "_blank");

		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				changeHistory(event.getValue());
			}
		});
	}

	private Widget initNavbar() {

		navbar.setStyleName(Constantes.NAVBAR_NAV_INVERSE);
		navbarInner.setStyleName(Constantes.NAVBAR_INNER);
		container.setStyleName(Constantes.CONTAINER_FLUID);
		projectName.setStyleName(Constantes.BRAND);

		projectName.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.ACCUEILWB);
			}
		});

		navbarForm.setStyleName(Constantes.NAVBAR_PULL_RIGHT);

		btnConnexion.setText(Constantes.CONNEXION);
        btnConnexion.setStyleName("navbar-text");
        btnConnexion.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                History.newItem(Constantes.CONNEXIONWB);
            }
        });

		btnDeconnexion.setText(Constantes.DECONNEXION);
        btnDeconnexion.setStyleName("navbar-text");
        btnDeconnexion.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                deconnexion();
            }
        });


        labelTitre.setStyleName("inscription");

		navbarForm.add(btnConnexion);
        navbarForm.add(labelTitre);
        navbarForm.add(btnDeconnexion);
        navCollapse.add(navbarForm);
		container.add(projectName);
		container.add(navCollapse);
		navbarInner.add(container);
		navbar.add(navbarInner);

		return navbar;
	}

	private void checkLogin() {
		final AirportServiceAsync connexionIface;
		connexionIface = GWT.create(AirportService.class);

        connexionIface.checkLogin(new AsyncCallback<UserBean>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
            public void onSuccess(UserBean result) {
                if (result != null) {
                    initPage();
                    initbouton(true);
                } else {
                    initPage();
                    initbouton(false);
                }
            }
		});
	}

	private void deconnexion() {

		final AirportServiceAsync connexionIface;
		connexionIface = GWT.create(AirportService.class);

		connexionIface.logout(new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Void result) {
				initbouton(false);
				History.newItem(Constantes.ACCUEILWB);
			}
		});

	}

	private void changeHistory(String historyToken) {
		switch (historyToken) {
            case Constantes.CONNEXIONWB:
                panelBody.clear();
                panelBody.add(new ConnexionWB());
                break;
            case Constantes.VOLCONSULTWB:
				panelBody.clear();
				panelBody.add(new VolWB(1));
				break;
			case Constantes.VOLAJOUTWB:
				panelBody.clear();
				panelBody.add(new VolWB(2));
				break;
			case Constantes.ACCUEILWB:
				panelBody.clear();
				panelBody.add(new AccueilWB());
				break;
			case Constantes.PANNEAUCONSULTWB:
				panelBody.clear();
				panelBody.add(new PanneauWB(1));
				break;
			case Constantes.PANNEAUAJOUTWB:
				panelBody.clear();
				panelBody.add(new PanneauWB(2));
				break;
			case Constantes.HOTELCONSULTWB:
				panelBody.clear();
				panelBody.add(new HotelWB(1));
				break;
			case Constantes.HOTELAJOUTWB:
				panelBody.clear();
				panelBody.add(new HotelWB(2));
				break;
            case "":
				panelBody.clear();
				panelBody.add(new AccueilWB());
				break;
			default:
				panelBody.clear();
				panelBody.add(new NotFoundWB());
				break;
        }
    }

    public static void initbouton(Boolean clientConnecter) {
        if (clientConnecter) {
			btnConnexion.setVisible(false);
            labelTitre.setVisible(true);
            btnDeconnexion.setVisible(true);
			sidebar.setVisible(true);
			span2.setStyleName("span2");
		} else {
			btnConnexion.setVisible(true);
            labelTitre.setVisible(false);
            btnDeconnexion.setVisible(false);
			sidebar.setVisible(false);
			sidebar.setVisible(false);
			span2.setStyleName("span1");
		}

	}

	private Widget initSideBar() {

		sidebar.setStyleName("well sidebar-nav");
		navlist.setStyleName("nav nav-list");
		navHeadVol.setStyleName("nav-header");
		dividerVol.setStyleName("divider");
		navHeadPanneau.setStyleName("nav-header");
		dividerPanneau.setStyleName("divider");
		navHeadHotel.setStyleName("nav-header");

		ajouterVolLi.add(ajouterVolLink);
		consulterVolLi.add(consulterVolLink);
		ajouterPanneauLi.add(ajouterPanneauLink);
		consulterPanneauLi.add(consulterPanneauLink);
		consulterHotelLi.add(consulterHotelLink);
		ajouterHotelLi.add(ajouterHotelLink);
		navHeadPanneau.getElement().setInnerHTML("<span class=\"icon-picture\"></span>  PANNEAU ");
		navHeadVol.getElement().setInnerHTML("<span class=\"icon-plane\"></span>  VOL");
		navHeadHotel.getElement().setInnerHTML("<span class=\"icon-home\"></span>  HOTEL");

		sidebar.add(navlist);
		navlist.add(navHeadPanneau);
		navlist.add(consulterPanneauLi);
		navlist.add(ajouterPanneauLi);
		navlist.add(dividerPanneau);
		navlist.add(navHeadVol);
		navlist.add(consulterVolLi);
		navlist.add(ajouterVolLi);
		navlist.add(dividerVol);
		navlist.add(navHeadHotel);
		navlist.add(consulterHotelLi);
		navlist.add(ajouterHotelLi);

		consulterVolLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.VOLCONSULTWB);
			}
		});

		ajouterVolLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.VOLAJOUTWB);
			}
		});

		ajouterPanneauLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.PANNEAUAJOUTWB);
			}
		});

		consulterPanneauLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.PANNEAUCONSULTWB);
			}
		});

		consulterHotelLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.HOTELCONSULTWB);
			}
		});

		ajouterHotelLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Constantes.HOTELAJOUTWB);
			}
		});

		return sidebar;

	}

	private void initPatienterPopUp() {
		final Image fullImage = new Image(resources.getPreloader());
		PatienterPopup.setWidget(fullImage);
		PatienterPopup.hide();
	}
}
